//操作标签组和用户信息，获取二维码和二维码链接
const config=require('./config')
const Wechat=require('./vchat').Wechat
const prefix='https://api.weixin.qq.com/cgi-bin/';
const api={
    newTag:prefix+'tags/create?',  //新增标签
    getTags:prefix+'tags/get?',  //获取所有
    editTag:prefix+'tags/update?',  //编辑
    delTag:prefix+'tags/delete?',  //删除
    getTagUsers:prefix+'user/tag/get?',  //获取标签下粉丝
    setUsersTag:prefix+'tags/members/batchtagging?',  //用户打上指定标签
    delUsersTag:prefix+'tags/members/batchuntagging?' , //为用户移除标签
    usersList:prefix+'user/get?',
    userDetails:prefix+'user/info/batchget?',
    ticket:prefix+'qrcode/create?',
}

let chat=new Wechat(config.wechat)
exports.tags={
    tagsOpt:async (dataType,postData) => {
        let url=''
        const body=dataType==='getTags'?{}:postData; //getTags时是不用提交任何数据
        switch(dataType){
            case 'newTag':
                url=api.newTag;
                break;
            case 'getTags':
                url=api.getTags;
                break;
            case 'editTag':
                url=api.editTag;
                break;
            case 'delTag':
                url=api.delTag;
                break;
            case 'getTagUsers':
                url=api.getTagUsers;
                break;
            case 'setUsersTag':
                url=api.setUsersTag;
                break;
            case 'delUsersTag':
                url=api.delUsersTag;
                break;
            case 'usersList'://获取所有已关注用户列表
                //next_openid=从第几个获取用户id，不填则默认第一个
                url=api.usersList;
                break;
            case 'userDetails':
                //next_openid=从第几个获取用户id，不填则默认第一个
                url=api.userDetails;
                break;
            case 'ticket':
                url=api.ticket;
                break;
        }
        return chat.getAcc().then(data=>{
            url+=`access_token=${data.access_token}`;
            return chat.getBody({url:url,method:'POST',body:body,json:true})
        })
    }
}
